package util;

public class Triangle {
		
		private boolean isTriangle = false;	//用于判断3个点去定的3个边是否能组成三角形
		
		private double a = 0.0d;	//边a
		private double b = 0.0d;	//边b
		private double c = 0.0d;	//边c
		private double l = 0.0d;	//周长
		private double s = 0.0f;	//面积
		
		//(x1,y1),(x2,y2),(x3,y3)分别为三角形三个顶点坐标
		public Triangle(double x1, double y1, double x2, double y2, double x3, double y3){
			Point p1 = new Point(x1,y1);
			Point p2 = new Point(x2,y2);
			Point p3 = new Point(x3,y3);
			a = getSide(p1,p2);	//利用两点求边
			b = getSide(p1,p3);
			c = getSide(p2,p3);
			this.isTriangle=JudgeisTriangle(a, b, c);
			if(isTriangle){	//如果是三角形再求周长和面积
				getL();
				getS();
			}
		}
		
		//利用两个点来获取边
		private double getSide(Point p1, Point p2){
			double sub1 = 0.0d;	//横坐标之差
			double sub2 = 0.0d;	//纵坐标之差
			sub1 = p1.x > p2.x ? p1.x - p2.x : p2.x - p1.x;
			sub2 = p1.y > p2.y ? p1.y - p2.y : p2.y - p1.y;
			return Math.sqrt((sub1*sub1 + sub2*sub2));
		}
		private boolean JudgeisTriangle(double a,double b,double c){
			double [] tmp = new double []{a,b,c};
			boolean is=false;
			MathUtil.bubbleSort(tmp);
			if ((tmp[0])<(tmp[1]+tmp[2])){
				is= true;
			}
			return is;
			
		}
		
		//获取周长
		private void getL(){
			l = a + b + c;
		}
		
		//获取面积
		public double getS(){
			double p = l/2;
			s = Math.sqrt(p*(p-a)*(p-b)*(p-c));
			return s;
		}
		
		public void print(){
			System.out.println("三边长为："+a+","+b+","+c);
			System.out.println("周长为："+l);
			System.out.println("面积为："+s);
		}
		
		//点类(仅供 Triangle 类使用的内部类)
		private class Point{
			public double x = 0;
			public double y = 0;
			public Point(double px, double py){
				x = px;
				y = py;
			}
		}
		
		public static void main(String[] args) {
			//求(1,1)、(1,4)、(5,1)三个坐标组成的三角形的周长和面积
			Triangle tri = new Triangle(0,0,2,0,1,0);//实例化一个三角形
			tri.print();	//输出属性
		}
}
